package com.ipad;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.ipad.ui.BaseActivity;
import com.ipad.ui.activity.IndexActivity;
import com.ipad.ui.activity.LoginActivity;
import com.ipad.ui_operation.OperationMainActivity;
import com.ipad.utils.SharePerenceUtils;

import java.util.List;

import butterknife.BindView;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
import pub.devrel.easypermissions.PermissionRequest;

public class MainActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks{
    @BindView(R.id.version_code)
    TextView version_code;
    private String token;
    Handler handler = new Handler();

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    public void initView() {
        PackageManager packageManager = getPackageManager();
        PackageInfo packageInfo = null;
        int versionCode = 1;
        String version_name = "";
        try {
            packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            versionCode = packageInfo.versionCode;
            version_name = packageInfo.versionName;
//            app_version.setText("版本号: "+version_name);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            versionCode = 0;
        }
        version_code.setText("v"+version_name);


        String[] perms = new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
                Manifest.permission.VIBRATE
        };

        if (EasyPermissions.hasPermissions(this,perms)){
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    toMainActivity();
                }
            }, 2000);
        }else {
            EasyPermissions.requestPermissions(this,getString(R.string.add_permission),0x11,perms);
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public void setToolbar() {

    }


    public boolean toMainActivity() {
        token = SharePerenceUtils.getToken(this);

        if (token == null || token.equals("")) {
            Intent intent = new Intent(this, LoginActivity.class);
            overridePendingTransition(0, 0);
            startActivity(intent);
            finish();
            return true;
        } else {
            if (getApplication().getPackageName().equals("com.ipad")) {
                Intent intent = new Intent(this, IndexActivity.class);
                overridePendingTransition(0, 0);
                startActivity(intent);
                finish();
            } else {
                Intent intent = new Intent(this, OperationMainActivity.class);
                overridePendingTransition(0, 0);
                startActivity(intent);
                finish();
            }

        }
        return false;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                toMainActivity();
            }
        }, 2000);
    }
    @AfterPermissionGranted(0x11)
    public void afterGranted(){
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                toMainActivity();
            }
        }, 2000);
    }
}
